/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.blockly.ohos.ui.spinner;

import com.google.blockly.ohos.ResourceTable;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;

public abstract class NiceSpinnerBaseAdapter<T> extends BaseItemProvider {

    private final PopUpTextAlignment horizontalAlignment;
    private final SpinnerTextFormatter spinnerTextFormatter;

    private Color textColor;
    private int backgroundSelector;

    int selectedIndex;

    NiceSpinnerBaseAdapter(
            Context context,
            Color textColor,
            int backgroundSelector,
            SpinnerTextFormatter spinnerTextFormatter,
            PopUpTextAlignment horizontalAlignment
    ) {
        this.spinnerTextFormatter = spinnerTextFormatter;
        this.backgroundSelector = backgroundSelector;
        this.textColor = textColor;
        this.horizontalAlignment = horizontalAlignment;
    }

    @Override
    public Component getComponent(int position, Component convertView, ComponentContainer parent) {
        Context context = parent.getContext();
        Text textView;

        if (convertView == null) {
            convertView = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_spinner_list_item, null, false);
            textView = (Text) convertView.findComponentById(ResourceTable.Id_text_view_spinner);

            ElementScatter elementScatter = ElementScatter.getInstance(context);
            Element element = elementScatter.parse(backgroundSelector);
            textView.setBackground(element);

            convertView.setTag(new ViewHolder(textView));
        } else {
            textView = ((ViewHolder) convertView.getTag()).textView;
        }

        textView.setText(spinnerTextFormatter.format(getItem(position)));
        textView.setTextColor(textColor);

        setTextHorizontalAlignment(textView);

        return convertView;
    }

    private void setTextHorizontalAlignment(Text textView) {
        switch (horizontalAlignment) {
            case START:
                textView.setTextAlignment(TextAlignment.START);
                break;
            case END:
                textView.setTextAlignment(TextAlignment.END);
                break;
            case CENTER:
                textView.setTextAlignment(TextAlignment.HORIZONTAL_CENTER);
                break;
        }
    }

    public int getSelectedIndex() {
        return selectedIndex;
    }

    public void setSelectedIndex(int index) {
        selectedIndex = index;
    }

    public abstract T getItemInDataset(int position);

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public abstract T getItem(int position);

    @Override
    public abstract int getCount();

    static class ViewHolder {
        Text textView;

        ViewHolder(Text textView) {
            this.textView = textView;
        }
    }
}
